package com.gopay.bis.cmbc.expay.operation;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.expay.CmbcExPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.MD5Util;
import com.gopay.remote.bank.ICertHelper;

/**
 * 民生同行代扣签名验签类
 * 
 * @CmbcAdPaySameCerthelper.java
 * @author zhengxue
 * @2015-11-12 上午10:39:43 www.gopay.com.cn Inc.All rights reserved.
 */
public class CmbcAdPaySameCerthelper implements ICertHelper {

    /**
     * 签名
     */
    public String sign(BaseMessage bsaeMessage) throws GopayException {
        CmbcExPayMessage message = (CmbcExPayMessage) bsaeMessage;
        try {
            BankLogUtils.printExPayPlainLog(message);
            String signMessage = MD5Util.sign(message.getPlainMessage() + message.getPrivateKeyPath()).toUpperCase();
            BankLogUtils.printExPaySignatureLog(message, signMessage);
            return signMessage;
        } catch (Exception e) {
            BankLogUtils.printExPaySignExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
        }

    }

    /**
     * 验签
     */
    public Object verify(BaseMessage bsaeMessage) throws GopayException {
        CmbcExPayMessage message = (CmbcExPayMessage) bsaeMessage;
        try {
            BankLogUtils.printExPayPlainLog(message);
            BankLogUtils.printExPaySignatureLog(message, message.getSignMessage());
            String signMessage = MD5Util.sign(message.getPlainMessage() + message.getPrivateKeyPath()).toUpperCase();
            BankLogUtils.printExPaySignatureLog(message, signMessage);

            if (!StringUtils.equals(signMessage, message.getSignMessage())) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            BankLogUtils.printExPaySignExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
        }
    }

}
